package com.ylcy.ads;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ylcy.ads.config.ENV;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取需要的权限
        getPermssion();

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        ENV.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        ENV.nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);//NotificationManager实例对通知进行管理
        ENV.alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "alarm";
            String channelName = "打卡消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public void getPermssion(){
        //检查是否获得权限
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.REORDER_TASKS) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this, "未获得系统切换应用授权，将无法完成连续调起应用的操作！", Toast.LENGTH_SHORT).show();
            //没有获得授权，申请授权
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.REORDER_TASKS)) {
                //弹窗解释为何需要该权限，再次请求权限
                Toast.makeText(MainActivity.this, "请授权！", Toast.LENGTH_SHORT).show();
                //跳转到应用设置界面
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            } else {
                //不需要解释为何需要授权直接请求授权
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.REORDER_TASKS}, 1);
            }
        } else {
            //获得授权
            Toast.makeText(MainActivity.this, "已获得系统切换应用授权！", Toast.LENGTH_SHORT).show();
        }
        //检查是否获得权限
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this, "未获得解锁授权，将无法完成连续调起应用的操作！", Toast.LENGTH_SHORT).show();
            //没有获得授权，申请授权
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WAKE_LOCK)) {
                //弹窗解释为何需要该权限，再次请求权限
                Toast.makeText(MainActivity.this, "请授权！", Toast.LENGTH_SHORT).show();
                //跳转到应用设置界面
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            } else {
                //不需要解释为何需要授权直接请求授权
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WAKE_LOCK}, 1);
            }
        } else {
            //获得授权
            Toast.makeText(MainActivity.this, "已获得解锁授权！", Toast.LENGTH_SHORT).show();
        }
        //检查是否获得权限
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.DISABLE_KEYGUARD) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this, "未获得系统切换应用授权，将无法完成连续调起应用的操作！", Toast.LENGTH_SHORT).show();
            //没有获得授权，申请授权
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.DISABLE_KEYGUARD)) {
                //弹窗解释为何需要该权限，再次请求权限
                Toast.makeText(MainActivity.this, "请授权！", Toast.LENGTH_SHORT).show();
                //跳转到应用设置界面
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            } else {
                //不需要解释为何需要授权直接请求授权
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.DISABLE_KEYGUARD}, 1);
            }
        } else {
            //获得授权
            Toast.makeText(MainActivity.this, "已获得系统切换应用授权！", Toast.LENGTH_SHORT).show();
        }

    }



}